var a =0;
 var total='';
 var _last = null;
apiready = function(){	

	var name = api.frameName;
	setbounces(name,true);
    var id = api.pageParam.shopId;
    getGoodsData(id);
    showitem(id)
    showUserCode(id);
    getPartakeRecord(id,true);
    
   
    //下拉刷新
    api.setCustomRefreshHeaderInfo({
            bgColor : '#f0f0f0',
            image: {
        pull: 'widget://image/refresh/1.png', 
        transform: [
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png',
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png',
            'widget://image/refresh/6.png',
            'widget://image/refresh/7.png'
        ],
        load: [ 
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png', 
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png'
        ]
    }
        }, function() {
            //下拉刷新被触发，自动进入加载状态，使用 api.refreshHeaderLoadDone() 手动结束加载中状态
            setTimeout(function(){
             
                showitem(id)
    	showUserCode(id);
        getGoodsData(id);
        getPartakeRecord(id,true);
       				 api.refreshHeaderLoadDone();
            }, 800);
           
      });
   
    
    //上拉加载
    api.addEventListener({

        name: 'scrolltobottom',
        extra: {
            threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
        }
    }, function (ret, err) {
        $("#more").show();
        if(a==0){
            getPartakeRecord(id,true);
        }else{
            var _page=parseInt($api.getStorage("currentPage"));
       
            if(_page+10<=total){
            getPartakeRecord(id,false,_page+10,_last);
            $api.setStorage("currentPage", _page+10); // 页码+1
            }else{
            //alert(total)
            if(total==0){
            $api.html($api.byId("more"),"--暂无内容--")
            
            }else{
            $api.html($api.byId("more"),"--已显示全部内容--")
            }
                
            }
        }  
       
    });

}

//获取参与记录
function getPartakeRecord(id,isReload,currentPage,last) {
	
    var pageSize = 10;
    currentPage=isReload?0:currentPage;
    last = isReload?0:_last;
    
    if(isReload){
        a=1;
    }
    
    api.ajax({
        url: 'http://wap.jqmao.cn/getPartakeRecord',
        method: 'post',
        data: {
            values: {id: id,pageSize: pageSize,currentPage:currentPage,last:last}
        }
    },function (ret, err) {		
//alert(JSON.stringify(ret))
        if (ret) {
        
       
        	 total = ret['total'];
        	
        	 _last = ret['last'];
        	 if($api.getStorage('last')&&_last!=_last){
        	 	return false;
        	 }
        	 $api.setStorage('last',_last);
        	 
        	//alert(JSON.stringify(ret['Rows']))
            var content = $api.byId('divRecordList');
            var tpl = $api.byId('partaketemplate').text;
            var tempFn = doT.template(tpl);
           
            api.hideProgress();

            if (isReload) {
                 $api.setStorage("currentPage", 0);        // 重新设置为0
                $api.html(content, tempFn(ret['item']));
                
            } else {
                $api.append(content, tempFn(ret['item']));
            }
            
        } else {
            //$api.html($api.byId("more"),"--网络异常，刷新重试--")
        }
    });
}


//展示当前猫购码

function showUserCode(id){

	
	var user = $api.getStorage('user');
	var uid = user.uid;
	
	api.ajax({
        url: 'http://wap.jqmao.cn/getUserCode',
        method: 'post',
        data: {
            values: {shopid: id,uid: uid}
        }
    }, function (ret, err) {
        if (ret) {
      
            var content = $api.byId('code-content');
            var tpl = $api.byId('buycode').text;
            var tempFn = doT.template(tpl);
            $api.html(content,tempFn(ret));                     
        } else {
            //alert(JSON.stringify(err))
        }
    });
}

function showitem(id){

	api.ajax({
        url: 'http://wap.jqmao.cn/getSid',
        method: 'post',
        data: {
            values: {shopId: id}
        }
    }, function (ret, err) {
        if (ret) {
       
       
            var content = $api.byId('detail-li');
            var tpl = $api.byId('list-template').text;
            var tempFn = doT.template(tpl);
            $api.html(content,tempFn(ret));                     
        } else {
            //alert(JSON.stringify(err))
        }
    });
}
function showtime(){

	 var aa = $api.byId('aa');
	 var v = $api.html(aa);
	
	 if(v=='01'){
	 	$api.html(aa,'09');
	 }else{
	 	$api.html(aa,'01');
	 }
}

//获取商品信息
function getGoodsData(id){
        api.ajax({
        url: 'http://wap.jqmao.cn/getGoodsData?id='+id,
        method: 'get',
        dataType: 'json'
    }, function (ret, err) {
        if (ret) {
            var content = $api.byId('g');
            var tpl = $api.byId('gt').text;
            var tempFn = doT.template(tpl);
            $api.html(content,tempFn(ret));
            //var fnTimeCountDown = $api.byId('fnTimeCountDown');
            var time =ret.Remaining_second;
            if(time){
            var res = $("#fnTimeCountDown").fnTimeCountDown(time);
            }
                   	
        } else {
            //alert(JSON.stringify(err))
        }
    });
    
}

	
    